const md5 = require('md5')
const query = require('../../utils/query')
const { PASSWORD_SALT } = require('../config/constant')

// 获取管理员数据
exports.getAdminData = async () => {
  let sql = `select admin_id,nickname from manager`
  let result = await query(sql)
  return result
}

// 获取管理员信息
exports.getAdminInfo = async (admin_id) => {
  // 1. 查数据库
  let sql = `select admin_id,username,avatar,nickname,add_date,last_login_date,gender from manager where admin_id  = ${admin_id}`
  let result = await query(sql)
  return result
}

// 检查登录信息是否存在
exports.getFindUser = async (username, password) => {
  password = md5(password + PASSWORD_SALT)

  // 操作数据库 ,编写sql
  let sql = `select * from manager where username = '${username}' and password = '${password}'`
  return await query(sql)
}

// 获取管理员全部信息
exports.getAdminInfoById = async (admin_id) => {
  let sql = `select * from manager where admin_id = ${admin_id}`
  return await query(sql)
}

// 更新头像
exports.uploadAvatar = async (admin_id, avatar) => {
  let sql = `update manager set avatar = '${avatar}' where admin_id = ${admin_id}`
  let result = await query(sql)
  return result
}

// 修改密码
exports.changePassword = async (admin_id, newPassword) => {
  let sql = `update manager set password = '${newPassword}' where admin_id = ${admin_id}`
  let result = await query(sql)
  return result
}

// 获取系统图标和标题
exports.logoInfo = async () => {
  let sql = `select logo_title,logo_img from setting`
  let result = await query(sql)
  return result[0]
}

// 更新系统标题
exports.updateTitle = async (logoTitle) => {
  let sql = `update setting set logo_title = '${logoTitle}'`
  let result = await query(sql)
  return result
}

// 更新系统图标
exports.updateImg = async (logoImg) => {
  console.log('~~~~~~~', logoImg)

  let sql = `update setting set logo_img = '${logoImg}'`
  let result = await query(sql)
  return result
}
